package com.atguigu.spzx.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class CorsConfig {

    //使用gateway提供的跨域filter解决跨域问题
    //只要设置允许跨域的响应头就可以解决
    @Bean
    public CorsWebFilter corsWebFilter(){

        //跨域配置
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource() ;
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);//允许携带cookie跨域访问
        config.addAllowedHeader("*");//允许携带的头
        config.addAllowedMethod("*");//允许的请求方式
        config.addAllowedOriginPattern("*"); //允许从哪些客户端服务器中发起跨域请求
        // 配置/** (所有请求) 使用config的跨域配置
         source.registerCorsConfiguration("/**" , config);



        return new CorsWebFilter(source);
    }

}
